<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use Session,Config;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\Agent\AgentService;
use Illuminate\Pagination\LengthAwarePaginator;

class IndexController extends Controller
{
    private $index = 'Agent.index';
    private $summary = 'Agent.summary';
    private $detail = 'Agent.detail';

    public function index()
    {
        return view($this->index);
    }

    /**
     * @return $this
     * 主页
     */
    public function summary(Request $request)
    {

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $agent =  new AgentService();
        $data = $agent->getUserInfo($where);

        if($data['code'] == '0'){
            $data = $data['data'];
        }else{
            $data = [];
        }

        return view($this->summary)->with('data',$data);
    }



    public function userList()
    {
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $agent =  new AgentService();
        $data = $agent->getUserList($where);
        if($data['code'] == '0'){
            $data = $data['data'];
        }else {
            $data = [];
        }
        return view('Agent.userlist')->with('data',$data);

    }


    /**
     * @return $this
     * 开户
     */
    public function openUser()
    {
        $agent =  new AgentService();
        $data = $agent->productList();

        if($data['code'] == '0'){
            $data = $data['data'];
        }else{
            $data = [];
        }
        return view('Agent.add')->with('data',$data);

    }



    public function doOpenUser(Request $request)
    {

        $where = [];
        $where = $request->except('_token');
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));

        $agent =  new AgentService();
        $data = $agent->OpenUser($where);

        if($data['code'] == '0'){
          return redirect('/userList');
        }else{
            echo '<script>javascript:history.back();alert("'.$data['msg'].'！");</script>';

        }

    }

    /**
     * @param Request $request
     * @return mixed
     * 续费
     */
    public function renewvip(Request $request)
    {

        $user_id = $request->input('user_id');

        //  获取用户信息
        $agent =  new AgentService();
        $res = $agent->userInfo($user_id);

        if($res['code'] == '0'){
            $arr = $res['data'];
        }else{
            $arr = [];
        }

//        dd($arr);
        $agent =  new AgentService();
        $data = $agent->productList();

        if($data['code'] == '0'){
            $data = $data['data'];
        }else{
            $data = [];
        }
        return view('Agent.renew')->with('arr',$arr)->with('data',$data);

    }


    public function doRenew(Request $request)
    {
        $where = [];

        $where = $request->except('_token');
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));


        $agent =  new AgentService();
        $data = $agent->RenewUser($where);


        if($data['code'] == '0'){
            return redirect('/userList');
        }else{
            echo '<script>javascript:history.back();alert("'.$data['msg'].'！");</script>';

        }

    }


    public function discount(Request $request)
    {
        $where = [];

        $where = $request->except('_token');
        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));


        $agent =  new AgentService();
        $data = $agent->discount($where);


        if($data['code'] == '0'){
            $data = $data['data'];
        }else{
            $data = [];
        }

        return view('Agent.discount')->with('data',$data);

    }





















}
